package com.qcby.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.qcby.dao.MovieCommentMapper;
import com.qcby.model.MovieComment;
import com.qcby.model.dto.MovieCommentShowVo;
import com.qcby.model.dto.MovieCommentShowVo2;
import com.qcby.model.dto.MovieCommentShowVo3;
import com.qcby.query.MovieCommentQuery;
import com.qcby.service.MovieCommentService;
import com.qcby.util.Page;

@Service
public class MovieCommentServiceImpl extends BaseServiceImpl<MovieCommentQuery, MovieComment> implements
        MovieCommentService {


    private MovieCommentMapper movieCommentDao;

    @Autowired
    public void setBrandDao(MovieCommentMapper movieCommentDao) {
        this.movieCommentDao = movieCommentDao;
        this.baseDao = movieCommentDao;
    }

    @Override
    public Page<MovieCommentShowVo> selectCommentByMid(Integer mid, Integer pageNo, Integer pageSize,
        String orderByCondition) {
        Integer startNum = (pageNo - 1) * pageSize;

        Page<MovieCommentShowVo> page = new Page<>();
        page.setPageNo(pageNo);
        page.setPageSize(pageSize);
        page.setStartNum(startNum);

        String orderBy;
        if ("0".equals(orderByCondition)) {
            orderBy = "comment_at DESC";
        } else if ("1".equals(orderByCondition)) {
            orderBy = "comment_score DESC";
        } else {
            throw new IllegalArgumentException("排序参数非法！");
        }
        List<MovieCommentShowVo> showVoList = movieCommentDao.selectCommentByMid(mid, orderBy, startNum, pageSize);
        Integer count = movieCommentDao.selectCommentByMidCount(mid);

        page.setTotalCount(count);
        page.setList(showVoList);
        page.setTotalPage(count % pageSize == 0 ? count / pageSize : (count / pageSize) + 1);
        return page;
    }

    @Override
    public Page<MovieCommentShowVo2> selectCommentByUid(Integer uid, Integer pageNo, Integer pageSize, String orderByCondition) {
        Integer startNum = (pageNo - 1) * pageSize;

        Page<MovieCommentShowVo2> page = new Page<>();
        page.setPageNo(pageNo);
        page.setPageSize(pageSize);
        page.setStartNum(startNum);

        String orderBy;
        if ("0".equals(orderByCondition)) {
            orderBy = "comment_at DESC";
        } else if ("1".equals(orderByCondition)) {
            orderBy = "comment_score DESC";
        } else {
            throw new IllegalArgumentException("排序参数非法！");
        }
        List<MovieCommentShowVo2> showVoList = movieCommentDao.selectCommentByUid(uid, orderBy, startNum, pageSize);
        Integer count = movieCommentDao.selectCommentByUidCount(uid);

        page.setTotalCount(count);
        page.setList(showVoList);
        page.setTotalPage(count % pageSize == 0 ? count / pageSize : (count / pageSize) + 1);
        return page;
    }

    @Override
    public Page<MovieCommentShowVo3> selectCommentAdmin(Integer uid, Integer mid, Integer pageNo, Integer pageSize,
        String orderByCondition) {
        Integer startNum = (pageNo - 1) * pageSize;

        Page<MovieCommentShowVo3> page = new Page<>();
        page.setPageNo(pageNo);
        page.setPageSize(pageSize);
        page.setStartNum(startNum);

        String orderBy;
        if ("0".equals(orderByCondition)) {
            orderBy = "comment_at DESC";
        } else if ("1".equals(orderByCondition)) {
            orderBy = "comment_score DESC";
        } else {
            throw new IllegalArgumentException("排序参数非法！");
        }
        List<MovieCommentShowVo3> showVoList = movieCommentDao.selectCommentAdmin(uid, mid, orderBy, startNum, pageSize);
        Integer count = movieCommentDao.selectCommentAdminCount(uid, mid);

        page.setTotalCount(count);
        page.setList(showVoList);
        page.setTotalPage(count % pageSize == 0 ? count / pageSize : (count / pageSize) + 1);
        return page;
    }
}
